<?php

namespace App\Http\Controllers;

use App\Apply;
use App\User;
use App\Craftsman;
use Illuminate\Http\Request;

class ApplyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Apply $apply)
    {
        return view('pages.apply',['apply' => $apply->with(['user:id,nickname','zone'])->paginate(10)]);
    }

    public function update(Request $request,$id)
    {
        $apply = Apply::find($id);
        $user = User::find($apply->user_id);
        switch ($request->status) {
            case 1:  // Passed
                $apply->update(['status' => 1]);
                $craftsman = Craftsman::create([
                    'zone_id' => $apply->zone->id,
                    'user_id' => $apply->user_id,
                    'rank' => $apply->rank,
                    'check_time' => now()->addHour(8)
                ]);
                $user->update([
                    'verified' => 1,
                    'craftsman_id' => $craftsman->id
                ]);
                return back()->withStatus(__('已通过该申请'));
                break;
            case 2:  // Rejected
                $apply->update(['status' => 2]);
                return back()->withStatus(__('已拒绝该申请'));
                break;
            default:
                $apply->update(['status' => 0]);
                return back();
                break;
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Apply  $apply
     * @return \Illuminate\Http\Response
     */
    public function destroy(Apply $apply)
    {
        //
    }
}
